package com.zzw.controller;

import com.zzw.service.RedisPersistenceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis持久化控制器
 * 提供Web接口管理和监控Redis持久化
 */
@Controller
@RequestMapping("/redis/persistence")
public class RedisPersistenceController {
    
    private static final Logger logger = LoggerFactory.getLogger(RedisPersistenceController.class);
    
    @Autowired
    private RedisPersistenceService redisPersistenceService;
    
    /**
     * 获取Redis持久化状态
     */
    @GetMapping("/status")
    @ResponseBody
    public Map<String, Object> getPersistenceStatus() {
        logger.info("获取Redis持久化状态");
        try {
            return redisPersistenceService.getPersistenceStatus();
        } catch (Exception e) {
            logger.error("获取Redis持久化状态失败", e);
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", "获取Redis持久化状态失败: " + e.getMessage());
            return error;
        }
    }
    
    /**
     * 触发RDB持久化
     */
    @PostMapping("/rdb/save")
    @ResponseBody
    public Map<String, Object> triggerRdbSave() {
        logger.info("触发RDB持久化");
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean success = redisPersistenceService.triggerRdbSave();
            result.put("success", success);
            result.put("message", success ? "RDB持久化已触发" : "RDB持久化触发失败");
        } catch (Exception e) {
            logger.error("触发RDB持久化失败", e);
            result.put("success", false);
            result.put("message", "触发RDB持久化失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 触发AOF重写
     */
    @PostMapping("/aof/rewrite")
    @ResponseBody
    public Map<String, Object> triggerAofRewrite() {
        logger.info("触发AOF重写");
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean success = redisPersistenceService.triggerAofRewrite();
            result.put("success", success);
            result.put("message", success ? "AOF重写已触发" : "AOF重写触发失败");
        } catch (Exception e) {
            logger.error("触发AOF重写失败", e);
            result.put("success", false);
            result.put("message", "触发AOF重写失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 更新Redis持久化配置
     */
    @PostMapping("/config/update")
    @ResponseBody
    public Map<String, Object> updateConfig(
            @RequestParam(value = "enableRdb", defaultValue = "true") boolean enableRdb,
            @RequestParam(value = "enableAof", defaultValue = "true") boolean enableAof) {
        
        logger.info("更新Redis持久化配置: RDB={}, AOF={}", enableRdb, enableAof);
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean success = redisPersistenceService.updatePersistenceConfig(enableRdb, enableAof);
            result.put("success", success);
            result.put("message", success ? "Redis持久化配置已更新" : "Redis持久化配置更新失败");
        } catch (Exception e) {
            logger.error("更新Redis持久化配置失败", e);
            result.put("success", false);
            result.put("message", "更新Redis持久化配置失败: " + e.getMessage());
        }
        
        return result;
    }
} 